<?php

use app\common\exception\MiException;

/**
 * @throws MiException
 */
function getUniPlatform(): string
{
    $platform = request()->header('uni-platform');
    if (!$platform) throw new MiException("缺少平台标识");
    return $platform;
}

/**
 * @throws MiException
 */
function getAppid(): string
{
    $appid = request()->header('appid', '');
    if (getUniPlatform() == 'mp-weixin' && !$appid){
        throw new MiException("运行在微信小程序必须带有appid标识");
    }
    return $appid;
}

/**
 * @throws MiException
 */
function checkSiteAuth($rule): bool
{

    // 获取当前用户
    $adminId = \app\site\service\SiteAuthService::instance()->getLoginId();

    // 超级管理员不验证权限
    $super = \think\facade\Config::get('app.super_admin');
    if($super == $adminId){
        return true;
    }

    // 验证权限
    $auth = Auth::instance();
    // check中会将所有字母转为小写
    if ($auth->check($rule, $adminId)){
        return true;
    }else{
        throw new MiException("无操作权限");
    }
}


